package com.service.impl;

import com.entity.PropertyProjectInfo;
import com.repository.PropertyMonthlyFinanceRepository;
import com.repository.PropertyProjectInfoRepository;
import com.service.PropertyProjectInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class PropertyProjectInfoServiceImpl implements PropertyProjectInfoService {

    @Autowired
    private PropertyProjectInfoRepository propertyProjectInfoRepository;

    @Autowired
    private PropertyMonthlyFinanceRepository propertyMonthlyFinanceRepository;

    @Override
    public PropertyProjectInfo save(PropertyProjectInfo propertyProjectInfo) {
        return propertyProjectInfoRepository.save(propertyProjectInfo);
    }

    @Override
    public void deleteById(Integer id) {
        PropertyProjectInfo propertyProjectInfo = findById(id);
        propertyProjectInfoRepository.deleteById(id);
        propertyMonthlyFinanceRepository.deleteAllByProjectName(propertyProjectInfo.getProjectName());
    }

    @Override
    public PropertyProjectInfo update(PropertyProjectInfo propertyProjectInfo) {
        return propertyProjectInfoRepository.save(propertyProjectInfo);
    }

    @Override
    @Transactional(readOnly = true)
    public PropertyProjectInfo findById(Integer id) {
        return propertyProjectInfoRepository.findById(id).orElse(null);
    }

    @Override
    @Transactional(readOnly = true)
    public List<PropertyProjectInfo> findAll() {
        return propertyProjectInfoRepository.findAll();
    }

    @Override
    @Transactional(readOnly = true)
    public Page<PropertyProjectInfo> findAll(Pageable pageable) {
        return propertyProjectInfoRepository.findAll(pageable);
    }

    @Override
    @Transactional(readOnly = true)
    public Page<PropertyProjectInfo> findByProjectNameContaining(String projectName, Pageable pageable) {
        return propertyProjectInfoRepository.findByProjectNameContaining(projectName, pageable);
    }
}